package com.github.cdisk.xtrans.ui.widget;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class TextDialog extends Dialog {

	protected String result;
	protected Shell shell;
	protected SourceViewer editor;
	protected FormData fd_styledText;

	public static void main(String[] args) {
		Shell shell2 = new Shell();
		TextDialog dialog = new TextDialog(shell2, SWT.CLOSE);
		String result = dialog.open(null, "select * from dual");
		System.out.println(result);
	}

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public TextDialog(Shell parent, int style) {
		super(parent, style);
		setText("编辑");
	}

	/**
	 * Open the dialog.
	 * @param text 
	 * 
	 * @return the result
	 */
	public String open(String title, String text) {
		createContents();
		if (StringUtils.isNotEmpty(title)) shell.setText(title);
		editor.getTextWidget().setText(text);

		/* 屏幕居中打开 */
		Rectangle bounds = Display.getDefault().getPrimaryMonitor().getBounds();
		Rectangle rect = shell.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shell.setLocation(x, y);

		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
		shell.setSize(680, 480);
		shell.setText("SQL编辑器");
		shell.setLayout(new FormLayout());

		editor = new SourceViewer(shell, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
//		sqlEditor.getTextWidget().setFont(JFaceResources.getTextFont());

		StyledText styledText = editor.getTextWidget();
		fd_styledText = new FormData();
		fd_styledText.top = new FormAttachment(0);
		fd_styledText.right = new FormAttachment(100);
		fd_styledText.left = new FormAttachment(0);
		styledText.setLayoutData(fd_styledText);
		styledText.addVerifyKeyListener(new VerifyKeyListener() {
			@Override
			public void verifyKey(VerifyEvent event) {
				if (event.stateMask == SWT.CTRL) {
					char character = (char) (event.character | '\u0040');
					switch (character) {
					case 'A':
						styledText.selectAll();
						break;
					case 'Y':
						editor.doOperation(ITextOperationTarget.REDO);
						break;
					case 'Z':
						editor.doOperation(ITextOperationTarget.UNDO);
						break;
					default:
						break;
					}
				}
			}
		});

		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = null;
				shell.dispose();
			}
		});
		fd_styledText.bottom = new FormAttachment(btnNewButton, -6);
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.bottom = new FormAttachment(100, -10);
		fd_btnNewButton.right = new FormAttachment(100, -10);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("  取  消  ");
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = editor.getTextWidget().getText();
				shell.dispose();
			}
		});
		FormData fd_button = new FormData();
		fd_button.top = new FormAttachment(btnNewButton, 0, SWT.TOP);
		fd_button.left = new FormAttachment(0, 10);
		button.setLayoutData(fd_button);
		button.setText("  确  定  ");

	}

}
